/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.syr.iis.web.model.DialogueActScheme;
import edu.syr.iis.web.model.DialogueActSchemeManager;

/**
 * @author kinoue
 *
 */
public class EnterDialogueActSchemeController extends SimpleFormController
{
    
    private DialogueActSchemeManager dialogueActSchemeManager = null;
    
    public static final String DASCH_ID = "daschemeId";
    public static final String DASCHS_KEY = "daschemes";
        
    /**
     * Returns a new instance of DialogueActScheme object if "dialogueActSchemeId" 
     * HTTP parameter is specified; other returns instance of object in database 
     * matching the value of the parameter.
     * @see DialogueActScheme
     */
    	
    @Override
	protected Object formBackingObject(HttpServletRequest request)
    {
	System.out.println("Entering EnterDialogueActSchemeController!");
	
	DialogueActScheme dialogueActScheme = null;
        
	if (request.getParameter(DASCH_ID) != null
                && request.getParameter(DASCH_ID).trim().length() > 0) 
	{
            int conversationId = Integer.parseInt(request.getParameter(DASCH_ID));
            dialogueActScheme = dialogueActSchemeManager.getDialogueActScheme(conversationId);
        } else 
        {
            dialogueActScheme = new DialogueActScheme();
        }

	System.out.println("Return converlationLabel with ID: " + dialogueActScheme.getDialogueActSchemeId());
        return dialogueActScheme;
    }   

    @Override
	protected Map referenceData(HttpServletRequest request) throws Exception
    {
        HashMap model = new HashMap();
        model.put(DASCHS_KEY, dialogueActSchemeManager.getDialogueActSchemes());
        return model;
    }

    @Override
	protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors)
    {
	DialogueActScheme dialogueActScheme = (DialogueActScheme) command;
	
	System.out.println("Entinering onSubmit with ");
	System.out.println(" id:    " + dialogueActScheme.getDialogueActSchemeId());
	System.out.println(" scheme: " + dialogueActScheme.getScheme());
	System.out.println(" desc: " + dialogueActScheme.getDescription());
        
        dialogueActSchemeManager.saveDialogueActScheme(dialogueActScheme);
        return new ModelAndView(getSuccessView());
    }

    public DialogueActSchemeManager getDialogueActSchemeManager()
    {
        return dialogueActSchemeManager;
    }

    public void setDialogueActSchemeManager(
    	DialogueActSchemeManager dialogueActSchemeManager)
    {
        this.dialogueActSchemeManager = dialogueActSchemeManager;
    }

}
